package com.running.mobile;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;
import com.isomorphic.datasource.DataSource;
import com.isomorphic.datasource.DataSourceManager;

/**
 * 导入客户的同时导入分类,设置imsi值
 * @author Administrator
 *
 */
public class CustomerDMI {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public DSResponse add(DSRequest request ,Map record)throws Exception
	{
		String category = (String) record.get("category");
		
		//分类字段有值时写入分类表中对应的ID
		if(!StringUtils.isBlank(category)){
			record.remove("category");
			//验证该分类是否存在
			DataSource categoryds = DataSourceManager.getDataSource("customer_categoryDS");
			Map map = new HashMap();
			map.put("corp_sid", record.get("corp_sid"));
			map.put("user_pk", record.get("user_pk"));
			map.put("name",category);
			Map saved = categoryds.fetchSingle(map);
			
			if(saved != null){
				//如果存在，修改为category_pk为分类id
				record.put("category_pk", saved.get("pk"));
			}else{
				//如果不存在，增加分类
				categoryds.add(map);
				saved = categoryds.fetchSingle(map);
				record.put("category_pk", saved.get("pk"));
			}
		}
		
		//增加imsi
/*		DataSource imsiDS = DataSourceManager.getDataSource("imsiDS");
		Map imsi = imsiDS.fetchById(record.get("telephone"));
		if(imsi != null){
			record.put("imsi", imsi.get("imsi"));
		}*/
		
		
		request.setValues(record);
		return request.execute();
		 
	}


}
